﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace WX.WMS.Common
{
    /// <summary>
    /// DataValidDetect summary
    /// </summary>
    public class DataValidDetect
    {
        #region IsNumber

        /// <summary>
        /// IsNumber
        /// </summary>
        /// <param name="o">object</param>
        /// <returns></returns>
        public static bool IsNumber(object o)
        {
            try
            {
                Convert.ToDouble(o);
                return true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return false;
            }
        }

        #endregion

        #region IsDate

        /// <summary>
        /// Is Date
        /// </summary>
        /// <param name="o">object</param>
        /// <returns></returns>
        public static bool IsDate(object o)
        {
            // \\d{4} meaning{0-9}{4}
            if (o != null)
            {
                if (Regex.IsMatch(o.ToString(), "\\d{4}-\\d{2}-\\d{2}") || Regex.IsMatch(o.ToString(), "\\d{4}-\\d{1}-\\d{2}") || Regex.IsMatch(o.ToString(), "\\d{4}-\\d{1}-\\d{1}") || Regex.IsMatch(o.ToString(), "\\d{4}-\\d{2}-\\d{1}"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region IsExceedLength

        /// <summary>
        /// is over the max length
        /// </summary>
        /// <param name="o">object</param>
        /// <param name="length">length</param>
        /// <returns></returns>
        public static bool IsExceedLength(object o, int length)
        {
            if (o != null)
            {
                if (o.ToString().Length > length)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region IsIncludeStr

        /// <summary>
        /// Enter the characters to determine whether or not contain the specified character
        /// </summary>
        /// <param name="strValue">source string</param>
        /// <param name="token">specified string</param>
        /// <returns></returns>
        public static bool IsIncludeStr(string strValue, string token)
        {
            if (strValue != null)
            {
                if (strValue.IndexOf(token) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region IsExistFile

        /// <summary>
        /// Is file exist
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsExistFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region IsValidDirectory

        public static bool IsValidDirectory(string path)
        {
            if (!System.IO.Directory.Exists(path))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

    }
}
